package net.j4love.httpcomponent.client.example.quickstart;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author He Peng
 * @create 2017-05-20 23:55
 * @update 2017-05-20 23:55
 * @updatedesc : 更新说明
 * @see
 */
public class HttpClientDemo {

    public static void main(String[] args) throws IOException {

        // HttpClient的最基本功能是执行HTTP方法。
        // 执行一个 HTTP方法涉及一个或多个HTTP请求/ HTTP响应交流,通常 由HttpClient内部处理。
        // 用户预计将提供一个请求对象 执行和HttpClient预计将请求发送到目标服务器返回一个
        // 相应的响应对象,或在执行失败的情况下抛出异常。

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.youdao.com/search?q=love");
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        char[] buf = new char[1000];
        StringBuilder builder = new StringBuilder();
        while (inputStreamReader.read(buf) != -1) {
            builder.append(buf);
        }
        System.err.println(builder.toString());

        try {
            Thread.sleep(1000 * 100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        httpClient.close();
    }
}
